package com.weasel.weixin.commons.message.bean;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.weasel.helper.DocumentConverter;
import com.weasel.weixin.commons.config.CommonConfig;
import com.weasel.weixin.commons.exception.AesException;
import com.weasel.weixin.commons.helper.WeixinEntryptHelper;
import org.apache.commons.io.IOUtils;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;

/**微信普通推送消息体
 * Created by Dylan on 2015/10/9.
 */
@XStreamAlias("xml")
public class WeixinPostCommonXmlMessage {

    @XStreamAlias("MsgId")
    private long MsgId;

    @XStreamAlias("ToUserName")
    private String toUserName; //开发者微信号

    @XStreamAlias("FromUserName")
    private String fromUserName; //发送方帐号（一个OpenID）

    @XStreamAlias("CreateTime")
    private long createTime; //消息创建时间 （整型）

    @XStreamAlias("MsgType")
    private String msgType; //消息类型，event

    @XStreamAlias("Content")
    private String content;  //文本消息内容

    @XStreamAlias("PicUrl")
    private String picUrl;  //图片链接

    @XStreamAlias("MediaId")
    private String mediaId; //语音消息媒体id，可以调用多媒体文件下载接口拉取数据。

    @XStreamAlias("Format")
    private String format;  //语音格式，如amr，speex等

    @XStreamAlias("ThumbMediaId")
    private String thumbMediaId; //视频消息缩略图的媒体id，可以调用多媒体文件下载接口拉取数据

    @XStreamAlias("Location_X")
    private double locationX; //地理位置维度

    @XStreamAlias("Location_Y")
    private double locationY;  //地理位置经度

    @XStreamAlias("Scale")
    private int scale;  //地图缩放大小

    @XStreamAlias("Label")
    private String label;  //地理位置信息

    @XStreamAlias("Title")
    private String title; //消息标题

    @XStreamAlias("Description")
    private String description; //消息描述

    @XStreamAlias("Url")
    private String url; //消息链接

    public long getMsgId() {
        return MsgId;
    }

    public void setMsgId(long msgId) {
        MsgId = msgId;
    }

    public String getToUserName() {
        return toUserName;
    }

    public void setToUserName(String toUserName) {
        this.toUserName = toUserName;
    }

    public String getFromUserName() {
        return fromUserName;
    }

    public void setFromUserName(String fromUserName) {
        this.fromUserName = fromUserName;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public String getMsgType() {
        return msgType;
    }

    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getPicUrl() {
        return picUrl;
    }

    public void setPicUrl(String picUrl) {
        this.picUrl = picUrl;
    }

    public String getMediaId() {
        return mediaId;
    }

    public void setMediaId(String mediaId) {
        this.mediaId = mediaId;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getThumbMediaId() {
        return thumbMediaId;
    }

    public void setThumbMediaId(String thumbMediaId) {
        this.thumbMediaId = thumbMediaId;
    }

    public double getLocationX() {
        return locationX;
    }

    public void setLocationX(double locationX) {
        this.locationX = locationX;
    }

    public double getLocationY() {
        return locationY;
    }

    public void setLocationY(double locationY) {
        this.locationY = locationY;
    }

    public int getScale() {
        return scale;
    }

    public void setScale(int scale) {
        this.scale = scale;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * 未加密的消息
     * @param inputStream
     * @return
     */
    public static WeixinPostCommonXmlMessage fromWeixinPostInputSteam(InputStream inputStream){

        return DocumentConverter.xmlToEntity(inputStream,WeixinPostCommonXmlMessage.class);
    }

    /**
     *
     * @param inputStream
     * @return
     */
    public static WeixinPostCommonXmlMessage fromWeixinPostEncryptedInputSteam(CommonConfig config,InputStream inputStream,String timestamp,String nonce) throws ParserConfigurationException, IOException, SAXException, AesException {

        String xml = WeixinEntryptHelper.decryptMsg(config,IOUtils.toString(inputStream,"utf-8"),timestamp,nonce);
        return DocumentConverter.xmlToEntity(xml,WeixinPostCommonXmlMessage.class);
    }

}
